package pl.com.margielewski.sttdatabase.sqltables;

import java.util.Date;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class SQLActualState {

	public static final String c_BOOKS = "Books";
	public static final int c_BOOKS_ID = 2;
	public static final String c_BOOKS_Options = "INTEGER";

	public static final String c_BROCHURES = "Brochures";
	public static final int c_BROCHURES_ID = 3;
	public static final String c_BROCHURES_Options = "INTEGER ";

	public static final String c_DATE = "Date";
	public static final int c_DATE_ID = 9;
	public static final String c_DATE_Options = "NUMERIC";

	public static final String c_HOURS = "Hours";
	public static final int c_HOURS_ID = 4;
	public static final String c_HOURS_Options = "TEXT";

	public static final String c_ID = "id";
	public static final int c_ID_ID = 0;
	public static final String c_ID_Options = "INTEGER PRIMARY KEY AUTOINCREMENT";

	public static final String c_MAGAZINES = "Magazines";
	public static final int c_MAGAZINES_ID = 6;
	public static final String c_MAGAZINES_Options = "INTEGER";

	public static final String c_REGURAL_READERS = "RegularReaders";
	public static final int c_REGURAL_READERS_ID = 5;
	public static final String c_REGURAL_READERS_Options = "INTEGER";

	public static final String c_STUDIES = "Studies";
	public static final int c_STUDIES_ID = 8;
	public static final String c_STUDIES_Options = "INTEGER";

	public static final String c_USER = "User";
	public static final int c_USER_ID = 1;
	public static final String c_USER_Options = "TEXT";

	public static final String c_VISITS = "Visits";
	public static final int c_VISITS_ID = 7;
	public static final String c_VISITS_Options = "INTEGER";

	public static final String tableName = "ActualState";
	public static final String z_createTable = "CREATE TABLE "
			+ tableName
			+ "( "
			+ String.format("%s %s, ", c_ID, c_ID_Options)
			+ String.format("%s %s, ", c_USER, c_USER_Options)
			+ String.format("%s %s, ", c_BOOKS, c_BOOKS_Options)
			+ String.format("%s %s, ", c_BROCHURES, c_BROCHURES_Options)
			+ String.format("%s %s, ", c_HOURS, c_HOURS_Options)
			+ String.format("%s %s, ", c_REGURAL_READERS,
					c_REGURAL_READERS_Options)
			+ String.format("%s %s, ", c_MAGAZINES, c_MAGAZINES_Options)
			+ String.format("%s %s, ", c_VISITS, c_VISITS_Options)
			+ String.format("%s %s, ", c_STUDIES, c_STUDIES_Options)
			+ String.format("%s %s", c_DATE, c_DATE_Options) + ");";
	public static final String z_getDataById = "Select * from " + tableName
			+ " where " + c_ID + "= {0} ;";
	private int books;
	private int brochures;
	private Date date;
	private int hours;
	private int id;
	private int magazines;
	private int reguralReaders;
	private int studies;
	private String user;
	private int visits;

	public SQLActualState() {

	}

	public SQLActualState(int id) {
		this.setId(id);
	}

	public int getBooks() {
		return this.books;
	}

	public int getBrochures() {
		return this.brochures;
	}

	public boolean getDataFromDB(SQLiteDatabase db) {
		Cursor cursor = null;
		if (this.getId() == 0) {
			cursor = db.query(tableName, null, null, null, null, null, null);
		} else {
			cursor = db.query(tableName, null, c_ID + "=?",
					new String[] { String.valueOf(this.getId()) }, null, null,
					null);
		}

		if (cursor != null && cursor.moveToFirst()) {
			this.setBooks(cursor.getInt(c_BOOKS_ID));
			this.setBrochures(cursor.getInt(c_BROCHURES_ID));
			this.setDate(new Date(cursor.getLong(c_DATE_ID)));
			this.setUser(cursor.getString(c_USER_ID));
			this.setHours(cursor.getInt(c_HOURS_ID));
			this.setId(cursor.getInt(c_ID_ID));
			this.setMagazines(cursor.getInt(c_MAGAZINES_ID));
			this.setReguralReaders(cursor.getInt(c_REGURAL_READERS_ID));
			this.setStudies(cursor.getInt(c_STUDIES_ID));
			this.setVisits(cursor.getInt(c_VISITS_ID));
			return true;
		}
		return false;
	}

	public Date getDate() {
		return this.date;
	}

	public int getHours() {
		return this.hours;
	}

	public int getId() {
		return this.id;
	}

	public int getMagazines() {
		return this.magazines;
	}

	public int getReguralReaders() {
		return this.reguralReaders;
	}

	public int getStudies() {
		return this.studies;
	}

	public String getUser() {
		return this.user;
	}

	public int getVisits() {
		return this.visits;
	}

	public long insert(SQLiteDatabase db) {
		ContentValues newTodoValues = new ContentValues();

		newTodoValues.put(c_BOOKS, this.getBooks());
		newTodoValues.put(c_BROCHURES, this.getBrochures());
		newTodoValues.put(c_DATE, this.getDate().getTime());
		newTodoValues.put(c_HOURS, this.getHours());
		newTodoValues.put(c_USER, this.getUser());
		newTodoValues.put(c_MAGAZINES, this.getMagazines());
		newTodoValues.put(c_REGURAL_READERS, this.getReguralReaders());
		newTodoValues.put(c_STUDIES, this.getStudies());
		newTodoValues.put(c_VISITS, this.getVisits());

		return db.insertOrThrow(tableName, null, newTodoValues);
	}

	public void setBooks(int books) {
		this.books = books;
	}

	public void setBrochures(int brochures) {
		this.brochures = brochures;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public void setHours(int hours) {
		this.hours = hours;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setMagazines(int magazines) {
		this.magazines = magazines;
	}

	public void setReguralReaders(int reguralReaders) {
		this.reguralReaders = reguralReaders;
	}

	public void setStudies(int studies) {
		this.studies = studies;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public void setVisits(int visits) {
		this.visits = visits;
	}
}